package com.zbcbms.common;

import com.zbcbms.entity.chart.BookBorrowChart;
import com.zbcbms.entity.chart.UserJoinTimeCountChart;
import com.zbcbms.entity.chart.UserRoleCountChart;
import com.zbcbms.entity.chart.BookTypeChart;
import com.zbcbms.service.BookService;
import com.zbcbms.service.impl.BookBorrowServiceImpl;
import com.zbcbms.service.impl.BookServiceImpl;
import com.zbcbms.service.impl.UserServiceImpl;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.chart.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author fengq
 */
public class GetChartCommon {
    private BookService bookService=new BookServiceImpl();

    public ObservableList<PieChart.Data> getBookBorrowChartData() {
        ObservableList<PieChart.Data> answer = FXCollections.observableArrayList();
        answer.addAll(new PieChart.Data("被借走书本数量", bookService.getCount("使用中")),
                new PieChart.Data("没有被借走", bookService.getCount("空闲")));
        return answer;
    }

    public ObservableList<PieChart.Data> getBookStatusChartData() {
        ObservableList<PieChart.Data> answer = FXCollections.observableArrayList();
        answer.addAll(new PieChart.Data("使用中", bookService.getCount("使用中")),
                new PieChart.Data("空闲", bookService.getCount("空闲")));
        return answer;
    }

    public ObservableList<PieChart.Data> getBookTypeChartData() {
        ObservableList<PieChart.Data> answer = FXCollections.observableArrayList();
        List<BookTypeChart> list = bookService.getCountByType();
        for(BookTypeChart bookTypeChart:list){
            answer.add(new PieChart.Data(bookTypeChart.getType(),bookTypeChart.getNums()));
        }
        return answer;
    }


    public ObservableList<XYChart.Series<String, Long>> getLineChartData() {
        ObservableList<XYChart.Series<String, Long>> answer = FXCollections
                .observableArrayList();
        XYChart.Series<String, Long> aSeries = new XYChart.Series<>();
        XYChart.Series<String, Long> bSeries = new XYChart.Series<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        String s = format.format(new Date());
        Date date = null;
        try {
            date = format.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, -1);
        Integer num = c.getActualMaximum(Calendar.DAY_OF_MONTH);
        Date m = c.getTime();
        String mon = format.format(m);
        String substring = mon+"-";
        aSeries.setName(mon.substring(0,7)+"月份借出数量变化");
        bSeries.setName(mon.substring(0,7)+"月份归还数量变化");
        for ( Integer i = 1; i <= num; i++) {
            String time=substring.concat(i.toString());
            Long count1 = new BookBorrowServiceImpl().getBorrowCountByTime(time);
            Long count2 = new BookBorrowServiceImpl().getReturnCountByTime(time);
            aSeries.getData().add( new XYChart.Data<>(i.toString(), count1));
            bSeries.getData().add( new XYChart.Data<>(i.toString(), count2));
        }
        answer.addAll(aSeries,bSeries);
        return answer;
    }

    public ObservableList<XYChart.Series<String,Number>> getBorrowTopStackedBarChart() {

        ObservableList<XYChart.Series<String, Number>> answer = FXCollections
                .observableArrayList();
        CategoryAxis xAxis = new CategoryAxis();
        NumberAxis yAxis = new NumberAxis();

        xAxis.setLabel("Month");
        xAxis.setMaxWidth(1);
        yAxis.setLabel("数量");

        XYChart.Series<String,Number> series1 = new XYChart.Series();
        series1.setName("借阅量top30");
        List<BookBorrowChart> borrowByBookNames = new BookBorrowServiceImpl().getBorrowByBookName();
        for (BookBorrowChart bookBorrowChart:borrowByBookNames){
            series1.getData().add(new XYChart.Data(bookBorrowChart.getBookName(), bookBorrowChart.getCount()));
        }
        answer.add(series1);
        return answer;
    }

    public ObservableList<PieChart.Data> getUserRoleChartData() {
        ObservableList<PieChart.Data> answer = FXCollections.observableArrayList();
        List<UserRoleCountChart> countCharts = new UserServiceImpl().getUserCountByRole();
        for (UserRoleCountChart userRoleCountChart:countCharts){
            answer.add(new PieChart.Data(userRoleCountChart.getRole(),userRoleCountChart.getCount()));
        }
        return answer;
    }

    public ObservableList<XYChart.Series<String,Number>> getUserJoinCountStackedBarChart() {
        ObservableList<XYChart.Series<String, Number>> answer = FXCollections
                .observableArrayList();
        CategoryAxis xAxis = new CategoryAxis();
        NumberAxis yAxis = new NumberAxis();
        xAxis.setMaxWidth(1);
        yAxis.setLabel("数量");
        XYChart.Series<String,Number> series1 = new XYChart.Series();
        series1.setName("用户加入情况最新20条");
        List<UserJoinTimeCountChart> list = new UserServiceImpl().getUserCountByJoinTime();
        for (UserJoinTimeCountChart userJoinTimeCountChart:list){
            series1.getData().add(new XYChart.Data(userJoinTimeCountChart.getJoinDate().toString(), userJoinTimeCountChart.getCount()));
        }
        answer.add(series1);
        return answer;

    }


}
